# Create your views here.

from contacto.forms import ContactForm
from django.core import mail
from django.core.mail import EmailMessage, send_mail
from django.http import HttpResponseRedirect
from django.shortcuts import render, redirect
from universidades.views import sidebar


def contact(request):
    if request.method == 'POST': # If the form has been submitted...
        form = ContactForm(request.POST) # A form bound to the POST data
        
        if form.is_valid(request):
            asunto = form.cleaned_data['asunto']
            mensaje = form.cleaned_data['mensaje']
            email = form.cleaned_data['email']
            cc_a_mi = form.cleaned_data['cc_a_mi']
            recipients = ['vinculaentorno@uca.edu.py']
            if cc_a_mi:
                recipients.append(email)
                
            send_mail(asunto, mensaje + "\n Email de contacto:" + email , 'vinculaentorno@uca.edu.py' , recipients)
            return redirect( '/plataforma/home' + "?message=Su mensaje ha sido enviado." )
        else:
            print("fail")
            
    menu= sidebar(request)
    return render(request, 'contact.html', {
        'menu' : menu,
    })

def envio(request):
    return HttpResponseRedirect('/plataforma/contact/')
